<?php namespace Phpcmf\Model\Order; // 这里需要把App改成项目目录名称

// 付款模型示例
class Order extends \Phpcmf\Model
{

    private $row;

    // 订单信息
    public function info($id) {

        $order = $this->table_site('order')->get($id);

        return $order;
    }

    // 下单
    public function checkout($order, $field, $item) {

        $save = [

            'pay_id' => 0, // 付款ID
            'pay_time' => 0, // 付款时间

            'deliver_name' => '', // 物流名称
            'deliver_sn' => '', // 发货ID
            'deliver_time' => 0, // 发货时间

            'receipt_time' => 0, // 收货时间

            'status' => 1, // 订单状态 0关闭 1已下单 2已付款 3已发货 9 已完成
            'inputtime' => SYS_TIME, //下单时间

        ];

        $data = array_merge($save, $order, $field);

        $rt = $this->table_site('order')->insert($data);
        if (!$rt['code']) {
            return $rt;
        }

        $data['id'] = $rt['code'];

        foreach ($item as $t) {
            $rt = $this->table_site('order_item')->insert([
                'oid' => $data['id'],
                'cid' => $t['id'],
                'mid' => $data['mid'],
                'uid' => (int)$data['uid'],
                'sell_uid' => (int)$t['uid'],
                'price' => $t['price'],
                'quantity' => $t['quantity'],
                'sn' => $t['sn'],
                'sku_name' => dr_array2string($t['sku_name']),
                'sku_value' => $t['sku_value'],
                'title' => $t['title'],
                'thumb' => $t['thumb'],
                'url' => $t['url'],
                'status' => 0,
                'inputtime' => 0,
            ]);
            if (!$rt['code']) {
                return $rt;
            }
        }

        return dr_return_data($data['id'], 'ok');
    }

    // 通过订单找商品
    public function get_item($oid) {
        return $this->table_site('order_item')->where('oid', $oid)->getAll();
    }


    //=======================订单状态变更处理


    // 付款成功
    public function pay($order_id, $pay_id) {

        if (!$order_id) {
            log_message('error', '订单付款回调失败：order_id值不存在（'.FC_NOW_URL.'）');
            return;
        }


        $order = $this->info($order_id);
        if (!$order) {
            log_message('error', '订单付款回调失败：订单（'.$order_id.'）不存在（'.FC_NOW_URL.'）');
            return;
        }

        $order['pay_id'] = $pay_id;
        $order['pay_time'] = SYS_TIME;

        // 付款状态
        $order['status'] = 2;

        $rt = $this->table_site('order')->update($order_id, [
            'pay_id' => $order['pay_id'],
            'pay_time' => $order['pay_time'],
            'status' => $order['status'],
        ]);
        if (!$rt['code']) {
            log_message('error', '订单（'.$order_id.'）付款回调失败：'.$rt['msg'].'（'.FC_NOW_URL.'）');
            return;
        }

        $config = \Phpcmf\Service::M('app')->get_config('order');
        if (isset($config['module'][$order['mid']]['quantity']) && $config['module'][$order['mid']]['quantity']) {
            // 减少库存量
            $goods = $this->table_site('order_item')->where('oid', $order_id)->getAll();
            if (!$goods) {
                log_message('error', '订单（'.$order_id.'）库存计算失败：没有找到订单对应的商品（'.FC_NOW_URL.'）');
            } else {
                foreach ($goods as $t) {
                    $data = $this->table_site($order['mid'])->get($t['cid']);
                    if ($data) {
                        // 找到商品
                        $update = [];
                        if ($t['sku_value']) {
                            $data['price_sku'] = dr_string2array($data['price_sku']);
                            if (!isset($data['price_sku']['value'][$t['sku_value']]) || !$data['price_sku']['value'][$t['sku_value']]) {
                                log_message('error', '订单（'.$order_id.'）库存计算失败：商品（'.$order['nid'].'-'.$t['cid'].'）sku信息查询失败（'.FC_NOW_URL.'）');
                            } else {
                                // sku库存值
                                $data['price_sku']['value'][$t['sku_value']]['quantity'] = max(0, $data['price_sku']['value'][$t['sku_value']]['quantity'] - $t['quantity']);
                                $update = [
                                    'price_sku' => dr_array2string($data['price_sku']),
                                    'price_quantity' => max($data['price_quantity'] - $t['quantity'], 0),
                                ];
                            }
                        }
                        !$update && $update = [
                            'price_quantity' => max($data['price_quantity'] - $t['quantity'], 0),
                        ];
                        // 没有销量统计
                        $this->table_site($order['mid'])->update($t['cid'], $update);
                    } else {
                        log_message('error', '订单（'.$order_id.'）库存计算失败：没有找到（'.$order['nid'].'-'.$t['cid'].'）商品（'.FC_NOW_URL.'）');
                    }
                }
            }
        }


        // 付款提醒
        \Phpcmf\Service::L('Notice')->send_notice('order_pay', $order);

        // 通知卖家
        $uid = $order['uid'] = $order['sell_uid'];
        $username = $order['username'] = $order['sell_username'];
        \Phpcmf\Service::L('Notice')->send_notice('order_pay2', $order);
        $order['uid'] = $uid;
        $order['username'] = $username;

        // 付款成功后的交易流程
        $step = intval($config['module'][$order['mid']]['ptype']);
        switch ($step) {
            case 1:
                //付款→发货→完成
                // 通知卖家发货
                $order['uid'] = $order['sell_uid'];
                $order['username'] = $order['sell_username'];
                \Phpcmf\Service::L('Notice')->send_notice('order_fahuo', $order);
                break;
            case 2:
                //付款→收货→完成
                $this->fahuo($order, [
                    'deliver_sn' => '自动发货',
                    'deliver_name' => '自动发货',
                ]);
                break;
            case 3:
                //付款→发货→收货→完成
                // 通知卖家发货
                $order['uid'] = $order['sell_uid'];
                $order['username'] = $order['sell_username'];
                \Phpcmf\Service::L('Notice')->send_notice('order_fahuo', $order);
                break;
            default:
                // 付款→完成
                $this->shouhuo($order, '付款后自动完成订单');
                break;
        }

        // 通知用户充值消息
        return dr_return_data($order['id'], 'ok');
    }

    // 订单收货操作
    public function shouhuo($order, $note) {

        $order['status'] = 9; // 订单交易完成
        $order['receipt_time'] = SYS_TIME;

        $rt = $this->table_site('order')->update($order['id'], [
            'status' => $order['status'],
            'receipt_time' => $order['receipt_time'],
        ]);
        if (!$rt['code']) {
            return $rt;
        }

        // 商品表状态
        $this->db->table(SITE_ID.'_order_item')->where('oid', $order['id'])->update([
            'status' => 1,
            'inputtime' => SYS_TIME,
        ]);

        // 为商户打钱
        \Phpcmf\Service::M('pay')->add_money($order['sell_uid'], $order['money']);
        // 增加一条记录
        \Phpcmf\Service::M('pay')->add_paylog([
            'uid' => $order['sell_uid'],
            'username' => $order['sell_username'],
            'touid' => '0',
            'tousername' => '',
            'mid' => 'order',
            'title' => dr_lang('订单['.$order['sn'].']交易成功'),
            'value' => $order['money'],
            'type' => 'system',
            'status' => 1,
            'result' => '订单编号：'.$order['sn'],
            'paytime' => SYS_TIME,
            'inputtime' => SYS_TIME,
        ]);

        // 通知提醒用户
        \Phpcmf\Service::L('Notice')->send_notice('order_shouhuo', $order);

        // 通知卖家
        $uid = $order['uid'] = $order['sell_uid'];
        $username = $order['username'] = $order['sell_username'];
        \Phpcmf\Service::L('Notice')->send_notice('order_shouhuo2', $order);
        $order['uid'] = $uid;
        $order['username'] = $username;

        // 创建日志
        $this->add_log($order, $note);

        // 通知用户发货消息
        return dr_return_data($order['id'], 'ok');
    }

    // 创建订单日志记录
    public function add_log($order, $log) {
        $this->table_site('order_log')->insert([
            'oid' => (int)$order['id'],
            'log' => (string)$log,
            'status' => (int)$order['status'],
            'inputtime' => SYS_TIME
        ]);
    }

    // 订单操作日志信息
    public function log($id) {
        return $this->table_site('order_log')->where('oid', $id)->getAll();
    }

    // 发货
    public function fahuo($order, $data) {

        $is_tx = 0;
        $update = [];
        if ($order['status'] != 3) {
            $update['deliver_time'] = SYS_TIME;
            $order['status'] = $update['status'] = 3;
            $is_tx = 1;
        }

        $update['deliver_name'] = $data['deliver_name'];
        $update['deliver_sn'] = $data['deliver_sn'];
        $update['name'] = $data['name'];
        $update['phone'] = $data['phone'];
        $update['address'] = $data['address'];

        $rt = $this->table_site('order')->update($order['id'], $update);
        if (!$rt['code']) {
            return $rt;
        }

        // 创建日志
        $this->add_log($order, $data['note']);

        if ($is_tx) {
            // 发货提醒
            \Phpcmf\Service::L('Notice')->send_notice('order_fahuo', $order);
            $config = \Phpcmf\Service::M('app')->get_config('order');
            if (intval($config['module'][$order['mid']]['ptype']) == 1) {
                // 发货→完成
                $this->wancheng($order, '发货后自动完成订单');
            }
        }

        // 通知用户发货消息
        return dr_return_data($order['id'], 'ok');
    }

    // 交易关闭
    public function close($order, $note) {

        if ($order['status'] == 0) {
            return dr_return_data(0, dr_lang('订单已经关闭')); // 已经关闭
        }

        // 判断是否满足退款条件

        // 变更状态
        $rt = $this->table_site('order')->update($order['id'], [
            'status' => 0,
        ]);
        if (!$rt['code']) {
            return $rt;
        }

        $order['status'] = 0;

        // 创建日志
        $this->add_log($order, $note);

        // 关闭提醒
        \Phpcmf\Service::L('Notice')->send_notice('order_close', $order);

        // 退钱操作
        // 退款到买家
        if ($order['money'] > 0 && $order['pay_id'] == 1) {
            $rt = $this->query('update `'.$this->dbprefix('member').'` set `money`=`money`+'.$order['money'].',`freeze`=`spend`-'.$order['money'].' where id='.$order['uid']);
            if ($rt['code']) {
                \Phpcmf\Service::M('pay')->add_paylog([
                    'mid' => 'order',
                    'uid' => $order['uid'],
                    'username' => $order['username'],
                    'touid' => 0,
                    'tousername' => '',
                    'title' => '订单退款',
                    'value' => $order['money'],
                    'type' => 'finecms',
                    'url' => dr_member_url('order/home/show', ['id' => $order['id']]),
                    'status' => 1,
                    'result' => '订单号：'.$order['sn'],
                    'paytime' => SYS_TIME,
                ]);
            }
        }

        // 通知用户发货消息
        return dr_return_data($order['id'], 'ok');
    }

    public function get_field($config, $mid) {

        $rt = [];
        $field = \Phpcmf\Service::L('cache')->get_file('order-field-'.SITE_ID);
        if (!$field) {
            return [];
        }

        foreach ($field as $t) {
            if (isset($config['field'][SITE_ID][$t['id']]) && in_array($mid, $config['field'][SITE_ID][$t['id']])) {
                $t['setting'] = dr_string2array($t['setting']);
                $rt[$t['fieldname']] = $t;
            }
        }

        return $rt;
    }

    // 判断用户是否购买了本商品
    public function member_is_buy($uid, $mid, $cid, $sku = '') {

        if (!$uid) {
            return 0;
        } elseif (!$mid) {
            return 0;
        } elseif (!$cid) {
            return 0;
        }

        $db = $this->table_site('order_item')
            ->where('uid', $uid)
            ->where('mid', $mid)
            ->where('cid', $cid)
            ->where('status', 1);
        if ($sku) {
            $db->where('sku_value', $sku);
        }

        return $db->counts();
    }

    // 列出购买记录
    public function get_buy_items($mid, $cid, $num = '') {

        if (!$mid) {
            return [];
        } elseif (!$cid) {
            return [];
        }

        $db = $this->table_site('order_item')
            ->where('mid', $mid)
            ->where('cid', $cid)
            ->where('status', 1)->order_by('inputtime desc');

        return $db->getAll($num);
    }


    public function cache($siteid = SITE_ID) {

        $field = \Phpcmf\Service::M()->db->table('field')->where('disabled', 0)->where('relatedname', 'order')->where('relatedid', $siteid)->orderBy('displayorder ASC,id ASC')->get()->getResultArray();

        // 写入缓存
        \Phpcmf\Service::L('cache')->set_file('order-field-'.$siteid, $field);

    }

}
